package com.invengo.module_check.ui.fragment.setting;

import android.app.Application;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.invengo.base.router.RouterActivityPath;
import com.invengo.module_check.data.repsitory.common.CommonRepsitory;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.interceptor.logging.LocalLogUtil;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;

/**
 * @author buoker
 * @version 1.0.0
 * @date 2023/10/24
 * @className java.lang.String
 */
public class SettingViewModel extends BaseViewModel<CommonRepsitory> {
    public ObservableField<String> cacheSize = new ObservableField<>();
    public SingleLiveEvent<Boolean> haveCache = new SingleLiveEvent<>();

    public ObservableBoolean offlineModel = new ObservableBoolean();
    public UIChangeObservables uc = new UIChangeObservables();
    public ObservableBoolean autoLogin = new ObservableBoolean(ACache.getInstance().getAsBoolean(AKey.IS_AUTO_LOGIN));//离在线模式


    public class UIChangeObservables {
        public SingleLiveEvent<Boolean> feature = new SingleLiveEvent<>();
        public SingleLiveEvent<Boolean> netModel = new SingleLiveEvent<>();
        public SingleLiveEvent<Boolean> rfidSetting = new SingleLiveEvent<>();
        public SingleLiveEvent<String> conformDialogEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Void> logoutEvent = new SingleLiveEvent<>();
    }

    /**
     * @param application
     */
    public SettingViewModel(@NonNull Application application) {
        super(application, CommonRepsitory.getInstance());
        offlineModel.set(ACache.getInstance().getAsBoolean(AKey.IS_OFFLINE_MODEL));
        getCacheSize();
    }

    private void getCacheSize() {
        new Handler().postDelayed(() -> {
            String size = "0B";
            try {
                size = LocalLogUtil.FormetFileSize();
                haveCache.postValue(true);
                cacheSize.set(size);
            } catch (Exception e) {
                haveCache.postValue(false);
                cacheSize.set(size);
            }
        },200);
    }

    public BindingCommand launchNetFragment = new BindingCommand(() -> {
        uc.netModel.setValue(false);
    });
    public BindingCommand launchFeatureFragment = new BindingCommand(() -> {
        uc.feature.setValue(false);
    });
    public BindingCommand launchRfidSettingFragment = new BindingCommand(() -> {
        uc.rfidSetting.setValue(false);
    });

    public BindingCommand showDeleteCommand = new BindingCommand(() -> {
        if (!cacheSize.get().equals("0B")) {
            uc.conformDialogEvent.postValue("清理缓存文件");
        } else {
            ToastUtils.showShort("暂无可清理的缓存！");
        }
    });
    public BindingCommand logoutCommand = new BindingCommand(() -> {
        model.cleanUser();
        getUC().getFinishActivityEvent().call();
        ARouter.getInstance().build(RouterActivityPath.Check.PAGER_LOGIN).navigation();
    });

    public BindingCommand conformConfig = new BindingCommand(() -> {
        getUC().getFinishActivityEvent().call();
        ARouter.getInstance().build(RouterActivityPath.Check.PAGER_LOGIN).navigation();
    });

    public void conformOpeartion() {
        Observable
                .just(1)
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> getUC().getShowLoadingEvent().call())
                .map(integer -> LocalLogUtil.deleteCache())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnComplete(() -> getUC().getDismissLoadingEvent().call())
                .subscribe(aBoolean -> getCacheSize());
    }

    /**
     * @param isChecked
     */
    public void autoLogin(boolean isChecked) {
        ACache.getInstance().put(AKey.IS_AUTO_LOGIN, isChecked);
        autoLogin.set(isChecked);
    }

}
